package com.raja.servingStation.common.util;


import com.raja.servingStation.common.exception.BusinessRuntimeException;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by liuyanjun
 * on 2017/9/6/006.
 */
public class AssertUtils {
    public static void assertNotNull(String s) throws BusinessRuntimeException {
        if(StringUtils.isEmpty(s)){
            throw new BusinessRuntimeException(ErrorCode.ERR_PARAM);
        }
    }

    public static void assertPrice(String str)throws BusinessRuntimeException {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            throw new BusinessRuntimeException(ErrorCode.ERR_PRICE_PATTERN);
        }
    }
    public static void assertDate(Date str)throws BusinessRuntimeException {
        if( str == null ){
            throw new BusinessRuntimeException(ErrorCode.ERR_DATE_PATTERN);
        }
    }
    public static void assertBigDecimal(BigDecimal str,int length)throws BusinessRuntimeException {
        if( str == null ){
            throw new BusinessRuntimeException(ErrorCode.ERR_PRICE_PATTERN);
        }else {
            if(str.toString().indexOf(".")>0){
                if(str.toString().substring(str.toString().indexOf(".")+1,str.toString().length()).length()>length){
                    throw new BusinessRuntimeException(ErrorCode.ERR_DATA_PATTERN);
                }
            }
        }
    }

    public static void assertNumber(String s) throws BusinessRuntimeException {

        try{
            if(StringUtils.isEmpty(s) || s == null){
                throw new BusinessRuntimeException(ErrorCode.ERR_PARAM);
            }
            Pattern pattern = Pattern.compile("[0-9]*");
            Matcher isNum = pattern.matcher(s);
            if( !isNum.matches() ){
                throw new BusinessRuntimeException(ErrorCode.ERR_NUMBER);
            }
        }catch(Exception e){
            throw new BusinessRuntimeException(ErrorCode.ERR_NUMBER);
        }

    }

    public static void assertPlateNo(String s) throws BusinessRuntimeException {
        try{
            Pattern pattern = Pattern.compile("^[\u4e00-\u9fa5|WJ]{1}[A-Z0-9]{7}$");
            Matcher isNum = pattern.matcher(s);
            Pattern pattern5 = Pattern.compile("[\\u4e00-\\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}");
            Matcher isNum5 = pattern5.matcher(s);
            if(  !isNum.matches() && !isNum5.matches() ){
                throw new BusinessRuntimeException(ErrorCode.ERR_PLATE_NUMBER);
            }
        }catch(Exception e){
            throw new BusinessRuntimeException(ErrorCode.ERR_PLATE_NUMBER);
        }

    }

    public static void assertIntegerNotNull(Integer i) throws BusinessRuntimeException {
        if(i==null){
            throw new BusinessRuntimeException(ErrorCode.ERR_PARAM);
        }

    }
    public static void assertLongNotNull(Long i) throws BusinessRuntimeException {
        if(i==null){
            throw new BusinessRuntimeException(ErrorCode.ERR_PARAM);
        }

    }
    /*验证手机号*/
    public static void assertTelephone(String telephone) throws BusinessRuntimeException {
        assertNotNull(telephone);
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4])|(18[0-9])|(17[0-9])|(147))\\d{8}$");
        Matcher m = p.matcher(telephone);
        if(!m.matches()){
            throw  new BusinessRuntimeException(ErrorCode.ERR_MOBILE_FORMAT);
        }
    }

    public static void assertFaultReasonNotNull(String s) throws BusinessRuntimeException {
        if(StringUtils.isEmpty(s) || s == null){
            throw new BusinessRuntimeException(ErrorCode.ERR_FALUT_REASON_NULL);
        }
    }

    public static void assertRepairNotNull(String s) throws BusinessRuntimeException {
        if(StringUtils.isEmpty(s) || s == null){
            throw new BusinessRuntimeException(ErrorCode.ERR_REPAIR_RESULT_NULL);
        }
    }

    public static void main(String[] args) {
        try {
            assertPlateNo("川1A1X35R");
        } catch (BusinessRuntimeException e) {
            e.printStackTrace();
        }
    }

    public static void assertFault(String sympton,String standardFaultId) throws BusinessRuntimeException {
        if(StringUtils.isEmpty(standardFaultId)&&StringUtils.isEmpty(sympton)){
            throw new BusinessRuntimeException(ErrorCode.MUST_CHOOSE_ONE_FAULTINFOR);
        }
    }
    public static void assertListNotNull(List list) throws BusinessRuntimeException {
            if(list ==null || list.size() == 0){
                throw new BusinessRuntimeException(ErrorCode.MUST_CHOOSE_ONE_FAULTINFOR);
        }
    }
}
